All files / web/src/app/api/admin/page-spots/[pageId]/[spotId]/refine-prompt route.ts

0% Statements 0/63
0% Branches 0/1
0% Functions 0/1
0% Lines 0/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                                                                                                                               
import { type NextRequest, NextResponse } from 'next/server'
import { z } from 'zod'
import { withAuth } from '@/lib/auth/withAuth'
import { getSpotDefinition } from '@/lib/page-spots/spotDefinitions'
import { loadSpotConfig } from '@/lib/page-spots/loadSpotConfig'
import { llm } from '@/lib/llm'
import { trackedCall } from '@/lib/ai-usage/llm-middleware'
import { AiFeature } from '@/lib/ai-usage/features'

const SYSTEM_CONTEXT = `You are an expert prompt engineer for AI image generation. You're writing prompts for Abaci.one — an educational platform teaching mental math through the soroban (Japanese abacus). The image will be displayed as a content spot on a marketing page. Improve the given prompt to be more vivid, specific, and effective for image generation, without changing the subject or adding concepts not already indicated.`

/**
 * POST /api/admin/page-spots/[pageId]/[spotId]/refine-prompt
 *
 * Uses an LLM to refine the current prompt for better image generation.
 */
export const POST = withAuth(
  async (_request: NextRequest, { userId, params }) => {
    const { pageId, spotId } = (await params) as { pageId: string; spotId: string }

    const def = getSpotDefinition(pageId, spotId)
    if (!def) {
      return NextResponse.json({ error: `Unknown spot: ${pageId}/${spotId}` }, { status: 404 })
    }

    const config = loadSpotConfig(pageId, spotId)
    if (!config || config.type !== 'generated') {
      return NextResponse.json(
        { error: 'Spot must be configured as "generated" type with a prompt' },
        { status: 400 }
      )
    }

    if (!config.prompt) {
      return NextResponse.json({ error: 'Spot has no prompt to refine' }, { status: 400 })
    }

    const response = await trackedCall(
      llm,
      {
        prompt: `${SYSTEM_CONTEXT}

Spot location: "${def.label}" — ${def.description}
${def.aspectRatio ? `Aspect ratio: ${def.aspectRatio}` : ''}

Current image prompt:
"${config.prompt}"

Provide an improved version of this prompt.`,
        schema: z.object({
          refined: z.string().describe('The improved image generation prompt'),
        }),
      },
      { userId, feature: AiFeature.PAGE_SPOT_REFINE }
    )

    return NextResponse.json({
      original: config.prompt,
      refined: response.data.refined,
    })
  },
  { role: 'admin' }
)